from flask import  url_for
from models import User, Post
import base64

def to_img(img_binary, mime_type="image/jpeg"):
    base64_str = base64.b64encode(img_binary).decode("utf-8")
    return f"data:{mime_type};base64,{base64_str}"

def Images_default():
    return [url_for('static', filename='default.jpg')]

def Images_uid(uid):
    user= User.get_by_id(uid)
    if not user or not user.img:
        return Images_default()
    return [to_img(user.img)]

def Images_pid(pid):
    post = Post.get_by_id(pid)
    if not post or not post.img:
        return Images_default()
    return [to_img(post.img)]
